﻿namespace com.stifel.ServerBuildMgmt
{
    using System.Linq;
    using System.Reflection;
    using System.Xml;

    using com.stifel.Extensions.AssemblyExtensions;
    using com.stifel.Extensions.SymbolExtensions;
    using com.stifel.ServerBuildMgmtDao;
    using com.stifel.SharePoint;

    using Microsoft.SharePoint;

    public class StifelOSRole_Definition : BaseDefinition
    {
        #region Fields

        const string LOOKUPSITECOLUMNS = "Stifel-OSRoleLookupFields.xml";
        const string SITECOLUMNS = "Stifel-OSRole.xml";

        #endregion Fields

        #region Constructors

        public StifelOSRole_Definition( SPFeatureReceiverProperties properties )
            : base(properties)
        {
        }

        ~StifelOSRole_Definition( )
        {
            Dispose( false );
        }

        #endregion Constructors

        #region Methods

        public override void Create( )
        {
            base.Create( );
            CreateSiteColumns( getFieldDefinitionsAsXml( SITECOLUMNS ) );
            CreateContentType( getContentTypeDefn( ) );
            CreateList( getListDefn( ) );
            CreateSiteColumns( getFieldDefinitionsAsXml( LOOKUPSITECOLUMNS ) );
            loadList( );
        }

        public override void Delete( )
        {
            base.Delete( );
            DeleteList( getListDefn( ), false );
            DeleteContentType( getContentTypeDefn( ) );
            DeleteSiteColumns( getFieldDefinitionsAsXml( SITECOLUMNS ) );
            DeleteSiteColumns( getFieldDefinitionsAsXml( LOOKUPSITECOLUMNS ) );
        }

        ContentTypeDefn getContentTypeDefn( )
        {
            StifelOSRole c = null;
            return new ContentTypeDefn
            {
                Name = Item.GetContentTypeName<StifelOSRole>( ),
                ContentTypeId = Item.GetContentTypeId<StifelOSRole>( ),
                WebLevel = true,
                Group = "Stifel",
                Description = "Describes an operating system role.",
                Fields = new FieldDefn[ ] {
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelOSRole>(c.GetPropertySymbol(o=>o.OSSubRoles))
                    },
                }.Distinct( ).ToList( )
            };
        }

        XmlNodeList getFieldDefinitionsAsXml( string resource )
        {
            XmlNodeList fields = null;
            string s = Assembly.GetExecutingAssembly( ).GetFullyQualifiedManifestResourceName( resource );
            string xml = Assembly.GetExecutingAssembly( ).GetManifestResourceAsString( s );
            if ( !string.IsNullOrEmpty( xml ) )
            {
                XmlDocument d = new XmlDocument( );
                d.LoadXml( xml );
                XmlNode n = d.SelectSingleNode( "Fields" );
                fields = n.SelectNodes( "Field" );
            }
            return fields;
        }

        ListDefn getListDefn( )
        {
            return new ListDefn
            {
                RootFolderName = StifelOSRoleDao.ListName,
                Title = "Stifel OS Roles",
                Description = "Contains the operating system roles.",
                ContentTypes = new string[ ] {
                    Item.GetContentTypeName<StifelOSRole>()
                }.ToList( )
            };
        }

        void loadList( )
        {
            StifelOSRoleDao dao = new StifelOSRoleDao( );
            dao.Clear( );
            dao.Add( new StifelOSRole
            {
                Title = "Active Directory Certificate Services"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Active Directory Domain Services"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Active Directory Federation Services"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Active Directory Lightweight Directory Services"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Active Directory Rights Management Services"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Application Server"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "DHCP Server"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "DNS Server"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Fax Server"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "File Services"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Hyper-V"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Network Policy and Access Services"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Print Services"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "UDDI Services"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Web Services (IIS)"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Windows Deployment Services"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Microsoft .NET Framework 3.0 Features"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "BitLocker Drive Encryption"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "BITS Server Extensions"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Connection Manager Administration Kit"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Desktop Experience"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Group Policy Management"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Internet Printing Client"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Internet Storage Name Server (iSNS)"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "LPR Port Monitor"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Message Queuing"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Multipath I/O"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Peer Name Resolution Protocol"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Quality Windows Audio Video Experience (qWave)"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Remote Assistance"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Remote Server Administration Tools"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Removable Storage Manager"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "RPC Over HTTP Proxy"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Services for NFS"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "SMTP Server"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Storage Manager for SANs"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Simple TCP/IP Services"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "SNMP Services"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Subsystem for UNIX-based Applications"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Telnet Client"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Telnet Server"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Trivial File Transfer Protocol (TFTP) Client"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Failover Clustering"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Network Load Balancing"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Windows Server Backup"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Windows System Resource Manager"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Windows Internet Naming Service (WINS) Server"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Wireless LAN Service"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Windows Internal Database"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Windows PowerShell"
            } );
            dao.Add( new StifelOSRole
            {
                Title = "Windows Process Activation Service"
            } );
        }

        #endregion Methods
    }
}